<template>
	<view ref="gyForm" class="gy-form" :class="{isCard: props.isCard}">
		<slot></slot>
	</view>
</template>

<script lang="ts" setup>
	import {
		ref,
		getCurrentInstance
	} from "vue";

	const props = defineProps({
		isCard: {
			type: Boolean,
			default: false
		}
	});

	const { proxy } : any = getCurrentInstance();

	const gyForm = ref(null);

	defineExpose({
		validate: (success ?: Function) => {
			let children = [];
			// #ifdef MP-WEIXIN
			children = proxy.$children;
			// #endif
			for (let i = 0; i < children.length; i++) {
				if (children[i].name && children[i].name === "gy-form-item" && children[i].props.show && children[i].props.required) {
					switch (children[i].props.inputType) {
						case "text":
							if (!children[i].props.modelValue) {
								proxy.utils.uni.toast(children[i].props.placeholder || "请输入" + children[i].props.label);
								return false;
							}
							break;
						case "phone":
							if (!children[i].props.modelValue) {
								proxy.utils.uni.toast(children[i].props.placeholder || "请输入" + children[i].props.label);
								return false;
							}
							if (!proxy.utils.verify.mobile(children[i].props.modelValue)) {
								proxy.utils.uni.toast("请输入合法手机号码");
								return false;
							}
							break;
						case "idcard":
							if (!children[i].props.modelValue) {
								proxy.utils.uni.toast(children[i].props.placeholder || "请输入" + children[i].props.label);
								return false;
							}
							if (!proxy.utils.idCard.verify(children[i].props.modelValue)) {
								proxy.utils.uni.toast("请输入合法身份证号");
								return false;
							}
							break;
						case "textarea":
							if (!children[i].props.modelValue) {
								proxy.utils.uni.toast(children[i].props.placeholder || "请输入" + children[i].props.label);
								return false;
							}
							break;
						case "select":
							if (!children[i].props.modelValue) {
								proxy.utils.uni.toast(children[i].props.placeholder || "请选择" + children[i].props.label);
								return false;
							}
							break;
						case "custom-label":
							if (!children[i].props.modelValue) {
								proxy.utils.uni.toast(children[i].props.placeholder || + children[i].props.label);
								return false;
							}
							break;
						case "custom-select":
							if (!children[i].props.modelValue) {
								proxy.utils.uni.toast(children[i].props.placeholder || "请选择" + children[i].props.label);
								return false;
							}
							break;
						case "custom-multiple-select":
							if (!children[i].props.modelValue || children[i].props.modelValue.length === 0) {
								proxy.utils.uni.toast(children[i].props.placeholder || "请选择" + children[i].props.label);
								return false;
							}
							break;
						case "switch":
							if (!children[i].props.modelValue) {
								proxy.utils.uni.toast(children[i].props.placeholder || "请选择" + children[i].props.label);
								return false;
							}
							break;
						case "radio":
							if (!children[i].props.modelValue) {
								proxy.utils.uni.toast(children[i].props.placeholder || "请选择" + children[i].props.label);
								return false;
							}
							break;
						case "images":
							if (!children[i].props.modelValue || children[i].props.modelValue.length === 0) {
								proxy.utils.uni.toast(children[i].props.placeholder || "请上传" + children[i].props.label);
								return false;
							}
							break;
						case "upload-idcards":
							if (children[i].props.inputModel === "all") {
								if (!children[i].props.modelValue[0]) {
									proxy.utils.uni.toast(children[i].props.placeholder || "请上传" + children[i].props.label + "人像面");
									return false;
								}
								if (!children[i].props.modelValue[1]) {
									proxy.utils.uni.toast(children[i].props.placeholder || "请上传" + children[i].props.label + "国徽面");
									return false;
								}
								if (!children[i].props.modelValue[2]) {
									proxy.utils.uni.toast(children[i].props.placeholder || "请上传" + children[i].props.label + "手持人像面");
									return false;
								}
							} else if (children[i].props.inputModel === "noHand") {
								if (!children[i].props.modelValue[0]) {
									proxy.utils.uni.toast(children[i].props.placeholder || "请上传" + children[i].props.label + "人像面");
									return false;
								}
								if (!children[i].props.modelValue[1]) {
									proxy.utils.uni.toast(children[i].props.placeholder || "请上传" + children[i].props.label + "国徽面");
									return false;
								}
							} else if (children[i].props.inputModel === "front") {
								if (!children[i].props.modelValue) {
									proxy.utils.uni.toast(children[i].props.placeholder || "请上传" + children[i].props.label + "人像面");
									return false;
								}
							} else if (children[i].props.inputModel === "back") {
								if (!children[i].props.modelValue) {
									proxy.utils.uni.toast(children[i].props.placeholder || "请上传" + children[i].props.label + "国徽面");
									return false;
								}
							}
							break;
						default:
							if (!children[i].props.modelValue) {
								proxy.utils.uni.toast(children[i].props.placeholder || "请输入" + children[i].props.label);
								return false;
							}
							break;
					}
				}
			}
			if (success) {
				success();
			}
		}
	});
</script>

<style lang="scss" scoped>
	.gy-form.isCard {
		margin: var(--gy-gap);
	}
</style>